from flask import url_for
from flask_restful import Resource

from model.crud import QueryMultipleInstances
from restful.data import DataDatetime
from restful.request import Get
from restful.response import ResponseSuccess
from restful.session import current_user_required, data


class Data(Resource):
    @current_user_required
    @data(DataDatetime())
    def get(self, data, current_user):
        likes_data = Get(url_for('userlike', uid=current_user.id, _external=True))
        follow_data = Get(url_for('userfollow', uid=current_user.id, _external=True))

        start = data.get('start')
        end = data.get('end')

        likes = []
        follows = []
        get_likes = []
        fans = []

        # 直接用返回的数据，不需要再获取 data 字段
        for lst, target in [
            (likes_data.get('likes', []), likes),
            (likes_data.get('get_likes', []), get_likes),
            (follow_data.get('follows', []), follows),
            (follow_data.get('fans', []), fans),
        ]:
            for item in lst:
                time_str = item.get('time')
                if not time_str:
                    continue
                # 如果start和end有值，才做比较
                if start and time_str < start:
                    continue
                if end and time_str > end:
                    continue
                target.append(item)

        return ResponseSuccess(data={
            'likes': likes,
            'get_likes': get_likes,
            'follows': follows,
            'fans': fans,
        })
